package com.cvcetic.ciudadverde;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.cvcetic.ciudadverde.adapters.CentrosCivAdapter;
import com.cvcetic.ciudadverde.asynctask.DownloadWebTask;
import com.cvcetic.ciudadverde.beans.Punto;
import com.cvcetic.ciudadverde.beans.PuntoBici;

public class PuntosBiciActivity extends ListActivity {
	private ProgressDialog progreso;
	private List<Punto> pb;
	private CentrosCivAdapter cca;
	private Aplicacion app;
	private Button actualizar;
	private static final int MAPA= 1;
	private static final int DETALLE= 2;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listcentrocivicos);
		pb = new ArrayList<Punto>();
		app = (Aplicacion) getApplication();
		
		conectar();
		cca = new CentrosCivAdapter(PuntosBiciActivity.this, pb);
		setListAdapter(cca);
		registerForContextMenu(this.getListView());
		actualizar=(Button)findViewById(R.id.act);
		actualizar.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				conectar();
				
			}
		});
	}

	public class WebTask extends AsyncTask<String, Void, List<Punto>> {

		

		@Override
		protected List<Punto> doInBackground(String... urls) {
			return DownloadWebTask.cogerPuntosBiciPage(app, urls);
		}

		@Override
		protected void onPostExecute(List<Punto> result) {
			progreso.dismiss();
			if (!result.isEmpty())
				pb=result;
				cca.setPbici(pb);
			Log.d("DebugPuntosBiciActivity", String.format(
					"Se recuperaron (%d)  centros civicos ", result.size()));
			cca.forceReload();

		}
	}

	public void conectar() {
		progreso = ProgressDialog.show(PuntosBiciActivity.this, "Conectando..",
				"Recuperando Datos.", true, false);
		WebTask task = new WebTask();
		String url = "http://www.vitoria-gasteiz.org/we001/was/we001Action."
				+ "do?idioma=es&menu=ciudad&menuInicio=menu10_07_07&urlDestino=/W11/WAS/disponibilidadAction.do";// getString(R.string.urlbicis);
		Log.d("DebugPuntosBiciActivity",
				String.format("La url para conectar es %s: ", url));
		task.execute(new String[] { url });
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		// Get the item that was clicked
		Object o = this.getListAdapter().getItem(position);
		String keyword = o.toString();
		Toast.makeText(this, "Tu seleccion es : " + keyword, Toast.LENGTH_SHORT)
				.show();

	}
	
	 
	/* Creates the menu items */
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	       super.onCreateContextMenu(menu, v, menuInfo);
	       menu.add(0, MAPA, 0, "Mapa");
	       menu.add(0, DETALLE, 0,  "Detalles");
	}
	 
	/* Handles item selections */
	public boolean onContextItemSelected(MenuItem item) {
	      AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	      PuntoBici centrociv=(PuntoBici)pb.get(info.position);
	      
	      switch (item.getItemId()) {
	           case MAPA:	 


	                 return true;
	           case DETALLE:

	        	  
	                 return true;
	           default:
	                 return super.onContextItemSelected(item);
	      }
	}
}
